/*
 * 导入外部js文件
 */
document.write("<script language=javascript src='/js/common.js'></script>");
var telephonePattern = /^(13[0-9]|14[5|7]|15[0-9]|18[0-9])\d{8}$/;
var error = true;

var currentStepId = 1;

function gainCode(){
	ajax({
		url: "/user/telephone/code",
		method: "GET",
		onsuccess: function(text){
			var result = eval("("+text+")");
			if(result.success){
				var codeBtn = document.getElementById("s1CodeBtn");
				codeBtn.className="verify-btn-disabled";
				codeBtn.disabled=true;
				
				var expired = 300*1000;
				var count = 1;
				var intervalId = setInterval(function(){
					codeBtn.innerHTML="已发送("+(expired-1000*count++)/1000+")";
				},1000);
				var timeoutId = setTimeout(function(){
					codeBtn.innerHTML="获取验证码";
					codeBtn.className="verify-btn";
					codeBtn.disabled=false;
					clearInterval(intervalId);
					clearTimeout(timeoutId);
				}, expired);
			}
		}
	});
}

function tostep2(){
	var code = document.getElementById("step1code").value;
	if(isEmptyString(code)){
		return ;
	}
	//异步验证
	ajax({
		url: "/code/verify",
		method: "POST",
		data: "code="+code,
		onsuccess: function(text){
			var result = eval("("+text+")");
			if(!result.success){
				document.getElementById("step1codeError").innerHTML = result.params.error;
			}else{
				document.getElementById("step1").style.display="none";
				document.getElementById("step2").style.display="block";
				document.getElementById("s1tos2").className="line-highlight";
				document.getElementById("circle2").className="circle-active";
				document.getElementById("textstep2").className="text-active";
			}
		}
	});
}

/**
 * 检测手机号码是否被使用
 * @param telephone
 * @returns
 */
function checkAvailable(telephone){
	var msg = document.getElementById("teleError");
	var codeBtn = document.getElementById("s2CodeBtn");
	if(!telephonePattern.test(telephone)){
		msg.innerHTML = "手机号码格式不正确";
		codeBtn.className="verify-btn-disabled";
		codeBtn.disabled=true;
		error = true;
		return ;
	}
	msg.innerHTML = "";
	ajax({
		method:"POST",
		url:"/telephone/check",
		data:"telephone="+telephone,
		onsuccess:function(responseText){
			var result = eval("("+responseText+")");
			if(!result.success){
				msg.innerHTML="telephone used";
				error = true;
			}else{
				codeBtn.className="verify-btn";
				codeBtn.disabled=false;
				error = false;
			}
		}
	});
}

function gainCodeInStep2(){
	var telephone = document.getElementById("telephone").value;
	ajax({
		url: "/telephone/code",
		method: "POST",
		data: "telephone="+telephone,
		onsuccess: function(text){
			var result = eval("("+text+")");
			if(result.success){
				var codeBtn = document.getElementById("s2CodeBtn");
				codeBtn.className="verify-btn-disabled";
				codeBtn.disabled=true;
				
				var expired = 300*1000;
				var count = 1;
				var intervalId = setInterval(function(){
					codeBtn.innerHTML="已发送("+(expired-1000*count++)/1000+")";
				},1000);
				var timeoutId = setTimeout(function(){
					codeBtn.innerHTML="获取验证码";
					codeBtn.className="verify-btn";
					codeBtn.disabled=false;
					clearInterval(intervalId);
					clearTimeout(timeoutId);
				}, expired);
			}
		}
	});
}

function tostep3(){
	var code = document.getElementById("step2code").value;
	var telephone = document.getElementById("telephone").value;
	if(isEmptyString(code)||error){
		return ;
	}
	//异步验证
	ajax({
		url: "/user/telephone/bind",
		method: "POST",
		data: "code="+code+"&telephone="+telephone,
		onsuccess: function(text){
			var result = eval("("+text+")");
			if(!result.success){
				alert(result.params.msg);
			}else{
				document.getElementById("step2").style.display="none";
				document.getElementById("s2tos3").className="line-highlight";
				document.getElementById("circle3").className="circle-active";
				document.getElementById("textstep3").className="text-active";
				alert(result.params.msg);
				document.location.href="/user/security";
			}
		}
	});
}